import { findAllCategory } from "../../api/category";
import { topCategory } from "../../api/constants";

export default {
  namespaced: true,
  state() {
    return {
      // 分类信息集合，依赖topCategory,防止白屏
      list: topCategory.map((item) => ({ name: item })),
    };
  },
  mutations: {
    setList(state, payload) {
      state.list = payload;
    },
    // 定义show，hide控制当前分类的二级分类的显示隐藏
    show(state, id) {
      const currentCategory = state.list.find((item) => item.id === id);
      currentCategory.open = true;
    },
    hide(state, id) {
      const currentCategory = state.list.find((item) => item.id === id);
      currentCategory.open = false;
    },
  },
  actions: {
    //获取分类数据
    async getList({ commit }) {
      const { result } = await findAllCategory();
      result.forEach((item) => {
        item.open = false;
      });

      // console.log(result);
      commit("setList", result);
    },
  },
};
